package net.lab1024.sa.base.module.support.soaraction.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.module.support.soaraction.dao.SoarActionDao;
import net.lab1024.sa.base.module.support.soaraction.domain.entity.SoarActionEntity;
import net.lab1024.sa.base.module.support.soaraction.domain.form.SoarActionAddForm;
import net.lab1024.sa.base.module.support.soaraction.domain.form.SoarActionQueryForm;
import net.lab1024.sa.base.module.support.soaraction.domain.form.SoarActionUpdateForm;
import net.lab1024.sa.base.module.support.soaraction.domain.vo.SoarActionVO;
import net.lab1024.sa.base.utils.DateUtilsLocal;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * chenyuqiang Service
 *
 * @Author chenyuqiang
 * @Date 2025-09-15 23:07:17
 * @Copyright 1
 */

@Service
public class SoarActionService {

    @Resource
    private SoarActionDao soarActionDao;

    /**
     * 分页查询
     */
    public PageResult<SoarActionVO> queryPage(SoarActionQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<SoarActionVO> list = soarActionDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    public int updateByEsid(SoarActionAddForm addForm) {

        return soarActionDao.updateByEsid(addForm.getEsid(), addForm.getAction(),addForm.getUpdateTime());
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(SoarActionAddForm addForm) {
        SoarActionEntity soarActionEntity = SmartBeanUtil.copy(addForm, SoarActionEntity.class);
        soarActionEntity.setId(UUID.randomUUID().toString());
        soarActionDao.insert(soarActionEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     */
    public ResponseDTO<String> update(SoarActionUpdateForm updateForm) {
        SoarActionEntity soarActionEntity = SmartBeanUtil.copy(updateForm, SoarActionEntity.class);
        soarActionEntity.setUpdateTime(DateUtilsLocal.getCurrentDateTimeStr());
        soarActionDao.updateById(soarActionEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        soarActionDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer id) {
        if (null == id) {
            return ResponseDTO.ok();
        }

        soarActionDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public void deleteByCreatime(String creatime) {
        soarActionDao.deleteByCreatime(creatime);
    }
}
